<template>
  <a-dropdown>
    <div class="header-avatar" style="cursor: pointer">
      <a-avatar
        class="avatar"
        size="small"
        shape="circle"
        :src="
          user.header_img
            ? user.header_img
            : 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png'
        "
      />
      <span class="name">{{ user.name }}</span>
    </div>
    <a-menu :class="['avatar-menu']" slot="overlay">
      <a-menu-item>
        <a-icon type="user" />
        <span @click="$router.push('/user/admininfo')">个人中心</span>
      </a-menu-item>
      <a-menu-divider />
      <a-menu-item @click="redo" v-if="hasToken">
        <a-icon type="redo" />
        <span>超管重置展示数据</span>
      </a-menu-item>
      <a-menu-divider v-if="hasToken" />
      <a-menu-item @click="logout">
        <a-icon style="margin-right: 8px" type="poweroff" />
        <span>退出登录</span>
      </a-menu-item>
    </a-menu>
  </a-dropdown>
</template>

<script>
import { logout } from '@/services/user';
import { mapMutations, mapGetters } from 'vuex';
export default {
  name: 'HeaderAvatar',
  computed: {
    ...mapGetters('account', ['user']),
  },
  created() {
    localStorage.getItem('e-token') && (this.hasToken = true);
  },
  beforeUpdate() {
    localStorage.getItem('e-token') && (this.hasToken = true);
  },
  data() {
    return {
      hasToken: false,
    };
  },
  methods: {
    ...mapMutations('account', ['setUser']),
    ...mapMutations('setting', ['setSystemName', 'setSystemLogo']),
    logout() {
      logout();
      this.$router.go(0);
    },
    redo() {
      localStorage.removeItem('e-token');
      const adminInfo = JSON.parse(localStorage.getItem('adminInfo'));
      this.hasToken = false;
      this.setUser(adminInfo);
      this.setSystemName('蓝景项目管理平台');
      // https://bluej.cn/newbluejsite/images/bluej_favicon.ico
      // this.setSystemLogo("https://bluej.cn/newbluejsite/images/bluej_favicon.ico");
      // 先清除sessionStorage的信息，防止受到上个用户的信息影响
      sessionStorage.clear();
      adminInfo.title
        ? sessionStorage.setItem('title', adminInfo.title)
        : sessionStorage.setItem('title', '蓝景项目管理平台');
      adminInfo.logo
        ? sessionStorage.setItem('logo', adminInfo.logo)
        : sessionStorage.setItem('logo', '');
      this.$message.success(`成功重置数据`);
      this.$router.push('/admin');
    },
  },
};
</script>

<style lang="less">
.header-avatar {
  display: inline-flex;
  .avatar,
  .name {
    align-self: center;
  }
  .avatar {
    margin-right: 8px;
  }
  .name {
    font-weight: 500;
  }
}
.avatar-menu {
  width: 150px;
}
</style>
